//
//  MyDate.m
//  NSDate上午下午
//
//  Created by dllo on 16/9/9.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "MyDate.h"

@implementation MyDate

- (NSInteger)totalDay {
    NSInteger month = _month.integerValue;
    NSInteger year = _year.integerValue;
    if (month <= 0 || month > 12) {
        return 0;
    }
    
    if (month == 2) {
        if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
            return 29;
        } else {
            return 28;
        }
    }
    if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
        return 31;
    } else if (month == 4 || month == 6 || month == 9 || month == 11) {
        return 30;
    }
    return 0;
}

// yyyy-MM-dd hh:mm:ss
- (instancetype)initWithDateString:(NSString *)dateString {
    if (self = [super init]) {
        _year = [dateString substringWithRange:NSMakeRange(0, 4)];
        _month = [dateString substringWithRange:NSMakeRange(5, 2)];
        _day = [dateString substringWithRange:NSMakeRange(8, 2)];
        _hour = [dateString substringWithRange:NSMakeRange(11, 2)];
        _minute = [dateString substringWithRange:NSMakeRange(14, 2)];
        _second = [dateString substringWithRange:NSMakeRange(17, 2)];
        _certainDay = [NSString stringWithFormat:@"%@%@%@", _year, _month, _day];
        if (_hour.integerValue >= 0 && _hour.integerValue < 6) {
            _bigHour = @"凌晨";
        } else if (_hour.integerValue >= 6 && _hour.integerValue < 12) {
            _bigHour = @"上午";
        } else if (_hour.integerValue >= 12 && _hour.integerValue < 18) {
            _bigHour = @"下午";
        } else if (_hour.integerValue >= 18 && _hour.integerValue < 24) {
            _bigHour = @"晚上";
        } else {
            _bigHour = @"这个是神马时间?";
        }
    }
    return self;
}

+ (instancetype)myDateWithDateString:(NSString *)dateString {
    return [[self alloc] initWithDateString:dateString];
}

- (instancetype)initWithDate:(NSDate *)date {
    NSDateFormatter *df = [[NSDateFormatter alloc] init];
    [df setDateFormat:@"yyyy-MM-dd hh:mm:ss"];
    NSString *dateString = [df stringFromDate:date];
    if (self = [self initWithDateString:dateString]) {        
    }
    return self;
}

+ (instancetype)myDateWithDate:(NSDate *)date {
    return [[self alloc] initWithDate:date];
}

- (NSString *)description
{
    return [NSString stringWithFormat:@"%@:%@%@%@%@%@%@", _bigHour, _year, _month, _day, _hour, _minute, _second];
}
@end
